# -*- coding: iso-8859-2 -*-

from authorshipanalyzer.textmodifier.mapper import EncodingMapper
from authorshipanalyzer.textmodifier.filter import CitationFilter
from authorshipanalyzer.textmodifier.filter import CharacterFilter
from authorshipanalyzer.textmodifier.filter import EncodingFilter
from authorshipanalyzer.textmodifier.filter import HTMLFilter
from authorshipanalyzer.textmodifier.filter import ValidationFilter
from authorshipanalyzer.textmodifier.filter import OtherMessageFilter

class MessagePreprocessor:
    '''
    Class responsible for preprocessing selected message
    @param editorList
    '''
    
    def __init__(self):
        '''
        Create list of all single message editors
        '''
        self.editorList = []
        self.editorList.append(EncodingMapper())
        self.editorList.append(CitationFilter())
        self.editorList.append(CharacterFilter())
        self.editorList.append(EncodingFilter())
        self.editorList.append(HTMLFilter())
        self.editorList.append(OtherMessageFilter())
        
    def preprocess(self, message):
        '''
        Filter all given messages
        @param: indicator if message is valid
        '''
        for editor in self.editorList:
            result = editor.editMessage(message)
            if result == False:
                return False
            
        if len(message.getText()) == 0:
            return False
        
        return True
    
if __name__ == "__main__":
    from authorshipanalyzer.file import Path
    from time import time
    import datetime
    import os
    from authorshipanalyzer.message import MessageReader
    
    fromDate = datetime.date(1995, 7, 6)
    toDate = datetime.date(2996, 5, 3)
    
    start = time()
    mr = MessageReader()
    messages = mr.readMessages(fromDate, toDate)
    lenPrev = len(messages)
    
    editorList = []
    editorList.append(EncodingMapper())
    editorList.append(CitationFilter())
    editorList.append(CharacterFilter())
    editorList.append(EncodingFilter())
    editorList.append(HTMLFilter())
    
    startAll = time()
    for editor in editorList:
        start = time()
        editor.editMessages(messages)
        end = time()
        lenAfter = len(messages)
        print str(editor) + " processed for: " + str(end - start) + " seconds"
        print str(editor) + " number of removed messages: " + str(lenPrev - lenAfter)
        
#    start = time()  
#    validationFilter = ValidationFilter()
#    validationFilter.removeNotValidMessages(messages)
#    end = time()
#    print str(validationFilter) + " processed for: " + str(end - start) + " seconds"
#          
    lenAfter = len(messages)
          
    endAll = time()
    print "Preprocessing stage taken: " + str(endAll - startAll) + " seconds"
    print "Processed messages: " + str(lenPrev)
    print "Number of removed messages: " + str(lenPrev - lenAfter)
    print "Current number of messages: " + str(lenAfter)
    